package com.xzakota.minesweeper.widget;

import com.xzakota.minesweeper.R;
import com.xzakota.minesweeper.controller.GameResource;
import com.xzakota.minesweeper.log.XLogger;
import com.xzakota.minesweeper.model.entity.UserBean;
import com.xzakota.minesweeper.widget.image.RoundImage;
import com.xzakota.minesweeper.widget.text.InputTextField;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.io.IOException;
import java.net.URL;

/**
 * 用户信息展示视图
 * 包括 用户头像、两个想要展示的数据
 *
 * @author Xzakota
 */
public class UserInfoPanel extends JPanel {
    private final InputTextField userTemp1Panel;
    private final InputTextField userTemp2Panel;

    public UserInfoPanel(UserBean user) {
        setLayout(new GridBagLayout());
        setBorder(new EmptyBorder(8, 8, 8, 8));

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 2;
        c.fill = GridBagConstraints.NONE;
        c.insets = new Insets(0, 5, 0, 5);
        c.anchor = GridBagConstraints.NORTHWEST;

        RoundImage userAvatar = new RoundImage();
        try {
            if (user.qq == null || user.qq.isEmpty()) {
                userAvatar.setAvatarImage(GameResource.getImageResource(R.Image.USER_AVATAR));
            } else {
                userAvatar.setAvatarImage(new URL("https://q1.qlogo.cn/g?b=qq&nk=" + user.qq + "&s=640"));
            }
        } catch (IOException e) {
            XLogger.error("获取用户头像失败: " + e);
        }
        add(userAvatar, c);

        JLabel usernameLabel = new JLabel(user.username);
        usernameLabel.setFont(new Font("黑体", Font.BOLD, 25));
        c.gridx = 2;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 1;
        add(usernameLabel, c);

        userTemp1Panel = new InputTextField();
        userTemp1Panel.setBackground(Color.BLACK);
        userTemp1Panel.setTagText("分数: ");
        userTemp1Panel.setFieldText("0");
        userTemp1Panel.setFieldEditable(false);
        c.gridx = 2;
        c.gridy = 1;
        c.gridwidth = 1;
        add(userTemp1Panel, c);

        userTemp2Panel = new InputTextField();
        userTemp2Panel.setTagText("胜场: ");
        userTemp2Panel.setFieldText("0");
        userTemp2Panel.setFieldEditable(false);
        c.gridx = 3;
        c.gridy = 1;
        add(userTemp2Panel, c);
    }

    public void setAccentColor(boolean is) {
        setOpaque(is);
        setBackground(is ? new Color(0xECFFFD) : null);
    }

    public void setInfo1Tag(String text) {
        userTemp1Panel.setTagText(text);
    }

    public void setInfo1Value(String text) {
        userTemp1Panel.setFieldText(text);
    }

    public void setInfo2Tag(String text) {
        userTemp2Panel.setTagText(text);
    }

    public void setInfo2Value(String text) {
        userTemp2Panel.setFieldText(text);
    }
}
